﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AreaOracle;
using Newtonsoft.Json;

namespace AreaMSSQL
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new XGQ_DBContext())
            {
                var allText = File.ReadAllText($@"D:\AreaData.txt");
                var steelAreas = JsonConvert.DeserializeObject<List<SteelArea>>(allText);

                var areas = context.Areas.ToList();
                
//                var provinces = steelAreas.Where(x => x.ProvinceId>0 && x.CityId <= 0 && x.CountyId <= 0 ).ToList();
//                var citys = steelAreas.Where(x => x.ProvinceId>0 && x.CityId > 0 && x.CountyId <= 0).ToList();
//                var countys = steelAreas.Where(x => x.ProvinceId>0 && x.CityId > 0 && x.CountyId> 0).ToList();
//                
//                var groupByProvince =( from a in steelAreas
//                    group a by a.ProvinceId
//                    into g
//                    select new
//                    {
//                        ProvinceId= g.Key,
//                        Guid=Guid.NewGuid()
//                    }).ToDictionary(r=>r.ProvinceId,r=>r.Guid);
//
//                var groupByCitys =( from a in steelAreas
//                    group a by a.CityId
//                    into g
//                    select new {
//                        CityId= g.Key,
//                        Guid=Guid.NewGuid()
//                    }).ToDictionary(r=>r.CityId,r=>r.Guid);
//
//                var groupByCountys =( from a in steelAreas
//                    group a by a.CountyId
//                    into g
//                    select new
//                    {
//                        CountyId= g.Key,Guid=Guid.NewGuid()
//                    }).ToDictionary(r=>r.CountyId,r=>r.Guid);
//                
//                
//              
//                steelAreas.ForEach(area =>
//                {
//                    var province = groupByProvince[area.ProvinceId];
//                    var city = groupByCitys[area.CityId];
//                    var county = groupByCountys[area.CountyId];
//
//                    area.ProvinceIdGuid =area.ProvinceId<=0?(Guid?) null: province;
//                    area.CityIdGuid =area.CityId<=0?(Guid?) null: city;
//                    area.CountyIdGuid =area.CountyId<=0?(Guid?) null: county;
//
//                    context.Areas.Add(new XGQ_Area()
//                    {
//                        IdentityId = Guid.NewGuid(),
//                        ProvinceId = area.ProvinceIdGuid,
//                        ProvinceName = area.ProvinceName,
//                        CityId = area.CityIdGuid,
//                        CityName = area.CityName,
//                        CountyId = area.CountyIdGuid,
//                        CountyName = area.CountyName,
//                        Code = area.Code,
//                        JP = area.JP,
//                        YwId = area.YwId,
//                        Sort = area.Sort,
//                        PinYin = area.PinYin,
//                        Tag = area.Tag,
//                    });
//                });

                
                steelAreas.ForEach(area =>
                {
                    context.Areas.Add(new XGQ_Area()
                    {
                        IdentityId = Guid.NewGuid(),
                        ProvinceId = area.ProvinceId,
                        ProvinceName = area.ProvinceName,
                        CityId = area.CityId,
                        CityName = area.CityName,
                        CountyId = area.CountyId,
                        CountyName = area.CountyName,
                        Code = area.Code,
                        JP = area.JP,
                        YwId = area.YwId,
                        Sort = area.Sort,
                        PinYin = area.PinYin,
                        Tag = area.Tag,
                    });
                   
                });
                
                context.SaveChanges();
            }
            
            Console.ReadKey();
        }
    }
}